package com.tsj.spzx.product.controller;

import com.tsj.spzx.model.entity.product.Category;
import com.tsj.spzx.model.entity.product.ProductSku;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.model.vo.h5.IndexVo;
import com.tsj.spzx.product.service.CategoryService;
import com.tsj.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/product/index")
@Tag(name = "首页接口")
//@CrossOrigin
public class IndexController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;

    /**
     * 获取首页数据
     * 1、获取到所有的一级分类
     * 2、获取到畅销商品 排名前十的（根据product_sku表中的sale_num字段）
     * @return
     */
    @GetMapping
    @Operation(summary = "获取首页数据")
    public Result<IndexVo> getIndexData(){
        // 1、获取到所有的一级分类
        List<Category> categoryList = categoryService.findOneCategory();
        // 2、获取到畅销商品
        List<ProductSku> productSkuList = productService.findProductBySale();
        final IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);
        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }
}
